<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BiodataControllerSiam extends MX_Controller {

    private $_template = "siam_iainmataram";

	function __construct()
	{
		parent::__construct();
        $this->access_library_siam->is_logged_in();
        $this->load->model('biodata_model');
    }

	function view()
	{
	    $data['biodata'] = $this->biodata_model->getDetailBiodata($this->session->userdata('siamunp_user'));
        $data['page'] = 'lihat_biodata';
        $this->load->view($this->_template,$data);
	}

    function edit()
	{
        $data['biodata'] = $this->biodata_model->getDetailBiodata($this->session->userdata('siamunp_user'));
        $data['select_options_sma'] = $this->system_model->getSelectOptionsValueSma();
        $data['select_options_agama'] = $this->system_model->getSelectOptionsValueAgama();

        $this->form_validation->set_rules('penghasilan_ayah', '', 'is_natural');
        $this->form_validation->set_rules('penghasilan_ibu', '', 'is_natural');
        $this->form_validation->set_message('is_natural', '*hanya karakter angka saja yang diperbolehkan');

        if ($this->form_validation->run($this) == FALSE)
        {
          $data['page'] = 'edit_biodata';
          $this->load->view($this->_template,$data);
        }
        else{
          $update = $this->biodata_model->updateDetailBiodata();

          if($update == TRUE)
          {
            $this->session->set_flashdata('status', 'Data telah disimpan');
            $this->session->set_flashdata('clr', 'p_messages_success');
          }
          else
          {
            $this->session->set_flashdata('status', 'Data gagal disimpan, ulangi lagi.');
            $this->session->set_flashdata('clr', 'p_messages_error');
          }

          redirect('siam/biodata.html', 'refresh');
        }

     }

     function cek_pass_baru()
     {
        if($this->input->post('pass_baru') == $this->input->post('pass_baru_2'))
        {
          $dataPass = $this->biodata_model->getPassMhs($this->session->userdata('siamunp_user'),$this->input->post('pass_lama'));

          if($dataPass == 1  && $this->input->post('pass_baru')!=NULL && $this->input->post('pass_baru_2')!=NULL)
	      {
            $this->biodata_model->updatePassMhs($this->session->userdata('siamunp_user'),$this->input->post('pass_baru'));

            $status = 1;
		  }
          else if($dataPass == 1 && $this->input->post('pass_baru')== NULL)
          {
            $status = 4;
          }
          else if($dataPass == 1 && $this->input->post('pass_baru_2')== NULL)
          {
            $status = 5;
          }
          else
          {
            $status = 2;
          }
        }
        else
          $status = 3;

        return $status;
     }

     function editPassword()
	 {
	    //$data['biodata'] = $this->biodata_model->getDetailBiodata($this->session->userdata('siamunp_user'));

        $status = $this->cek_pass_baru();

        if($this->input->post('key') != 1)
          $status = 0;

        switch ($status)
        {
          case 1:

             $data['message'] = 'Data telah disimpan. Simpan data akun Anda dengan baik.';
             $data['clr'] = 'p_messages_success';
             break;

          case 2:

             $data['message'] = 'Password lama salah.';
             $data['clr'] = 'p_messages_error';
             break;

          case 3:

             $data['message'] = 'Ulangi Re-password baru';
             $data['clr'] = 'p_messages_error';
             break;

          case 4:

             $data['message'] = 'Password baru tidak boleh kosong.';
             $data['clr'] = 'p_messages_error';
             break;

          case 5:

             $data['message'] = 'Field Re-password baru tidak boleh kosong.';
             $data['clr'] = 'p_messages_error';
             break;

          default:

             $data['message'] = NULL;
             break;
        }

        $data['page'] = 'ganti_password';
        $this->load->view($this->_template,$data);
	 }
}

